package com.quanyan.stadium.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.vo.NearbyUser;
import com.quanyan.stadium.service.GameEventService;
import com.quanyan.stadium.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by lei.w on 2016/7/5.
 */
@RestController
@RequestMapping("/api/stadium/userCenter")
public class UserCenterController {

    @Autowired
    GameEventService gameEventService;
    @Autowired
    UserCenterService userCenterService;

    @RequestMapping(value = "/queryUserGameSchedule", method = RequestMethod.POST)
    public APIResponse queryUserGameSchedule(HttpServletRequest request, @RequestBody Map<String, Object>  map) {

        // 获取Uid  涉及用户登录token
        Integer uid = (Integer) request.getAttribute("uid");

        Integer pageNum = (Integer) map.get("pageNo");
        Integer pageSize = (Integer) map.get("pageSize");


        if (ObjectUtil.isObjectEmpty(uid, pageNum, pageSize)) {
            return APIResponse.returnFail("param uid or pageNo or pageSize is null");
        }

        return APIResponse.returnSuccess(PageObj.create(0, pageNum, pageSize, userCenterService.queryUserGameEventByUidAndStatus(uid, 0, pageNum, pageSize)));
    }

    @RequestMapping(value = "/queryUserGameRecord", method = RequestMethod.POST)
    public APIResponse queryUserGameRecord(HttpServletRequest  request, @RequestBody Map<String, Object>  map) {

        Integer uid = (Integer) request.getAttribute("uid");
        Integer pageNum = (Integer) map.get("pageNo");
        Integer pageSize = (Integer) map.get("pageSize");

        System.out.println("queryUserGameEvent uid:"+uid);

        if (ObjectUtil.isObjectEmpty(uid, pageNum, pageSize)) {
            return APIResponse.returnFail("param uid or pageNo or pageSize is null");
        }

        if (ObjectUtil.isObjectEmpty(uid)) {
            return APIResponse.returnFail("param uid is null");
        }

        return APIResponse.returnSuccess(PageObj.create(0, pageNum, pageSize, userCenterService.queryUserGameRecord(uid, 1, pageNum, pageSize)));
    }

    @RequestMapping(value = "/queryUserInfo", method = RequestMethod.POST)
    public APIResponse queryUserInfo(HttpServletRequest  request, @RequestBody Map<String, Object>  map) {

        Integer uid = (Integer) request.getAttribute("uid");
        Integer categoryId = (Integer)map.get("categoryId");

        //参数校验
        if (ObjectUtil.isObjectEmpty(uid, categoryId)) {
            return APIResponse.returnFail("param is null");
        }

        System.out.println("queryUserInfo uid:"+uid+" categoryId:"+categoryId);

        return APIResponse.returnSuccess(userCenterService.queryUserInfo(uid, categoryId));
    }

    @RequestMapping(value = "/queryNearbyUserInfo", method = RequestMethod.POST)
    public APIResponse queryNearbyUserInfo(HttpServletRequest  request, @RequestBody NearbyUser nearbyUser) {

        Integer uid = (Integer) request.getAttribute("uid");

        // 参数校验
        if (ObjectUtil.isObjectEmpty(nearbyUser.getPageNo(),nearbyUser.getPageSize(),nearbyUser.getType(),nearbyUser.getCategoryId())) {
            return APIResponse.returnFail("param is null");
        }

        if (nearbyUser.getType().byteValue() == GameConstants.TEAM_TYPE_PERSON ) {
            if (ObjectUtil.isObjectEmpty(nearbyUser.getLat(),nearbyUser.getLng())) {
                return APIResponse.returnFail("param is null");
            }
        }

        if (nearbyUser.getType().byteValue() == GameConstants.TEAM_TYPE_CLUB) {
            if (ObjectUtil.isObjectEmpty(nearbyUser.getCityId())) {
                return APIResponse.returnFail("param is null");
            }
        }

        return APIResponse.returnSuccess(userCenterService.queryNearbyUser(uid, nearbyUser));
    }

    @RequestMapping(value = "/getGameEventList",method = RequestMethod.POST)
    public APIResponse<?> getGameEventList(HttpServletRequest  request,@RequestBody Map map) {
        Integer uid = (Integer) request.getAttribute("uid");
        Object pageNum = map.get("pageNum");
        Object pageSize = map.get("pageSize");
        if(null == pageNum || null == pageSize){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return APIResponse.returnSuccess(userCenterService.getMyGameEventList(uid, (Integer) map.get("pageNum"), (Integer) map.get("pageSize")));
    }

}